//---------------------------------------------------------------------------------
// Copyright (c) 2011, Microsoft Corporation
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//---------------------------------------------------------------------------------

namespace Microsoft.Samples.ServiceBus.WindowsPhoneChat.ViewModels
{
    using System;
    using System.Collections.Generic;
    using System.Collections.ObjectModel;
    using System.Linq;
    using Microsoft.Samples.ServiceBus.Messaging;
    using Microsoft.Samples.ServiceBus.WindowsPhoneChat.Commands;

    public class ChatTopicListControlViewModel : ViewModel
    {
        private readonly ObservableCollection<ChatTopicViewModel> items;

        public ChatTopicListControlViewModel()
        {
            this.items = new ObservableCollection<ChatTopicViewModel>();
        }

        public IEnumerable<ChatTopicViewModel> Items
        {
            get { return this.items; }
        }

        public ChatTopicViewModel AddTopic(TopicClient topic, bool tryLoadSubscription = true)
        {
            ChatTopicViewModel topicViewModel = null;

            if (!this.items.Any(c => string.Equals(c.Name, topic.Path, StringComparison.InvariantCultureIgnoreCase)))
            {
                topicViewModel = new ChatTopicViewModel(topic, this);
                this.items.Add(topicViewModel);

                // load the subscription for this topic if one exists
                if (tryLoadSubscription && Commands.GetSubscription.CanExecute(topicViewModel))
                {
                    Commands.GetSubscription.Execute(topicViewModel);
                }
            }

            return topicViewModel;
        }

        public void Remove(ChatTopicViewModel item)
        {
            if (this.items.Contains(item))
            {
                this.items.Remove(item);
            }
        }

        internal void UpdateItems(IEnumerable<TopicClient> topics)
        {
            var addedTopics = topics.Where(newTopic => !this.items.Any(existingTopic => string.Equals(existingTopic.Name, newTopic.Path, StringComparison.InvariantCultureIgnoreCase)));
            foreach (var topic in addedTopics.ToArray())
            {
                this.AddTopic(topic, false);
            }

            var removedTopics = this.items.Where(existingTopic => !topics.Any(newTopic => string.Equals(newTopic.Path, existingTopic.Name, StringComparison.InvariantCultureIgnoreCase)));
            foreach (var topic in removedTopics.ToArray())
            {
                this.items.Remove(topic);
            }

            foreach (var topicViewModel in this.items)
            {
                if (!addedTopics.Contains(topicViewModel.TopicClient))
                {
                    // load the subscription for this topic if one exists
                    if (Commands.GetSubscription.CanExecute(topicViewModel))
                    {
                        Commands.GetSubscription.Execute(topicViewModel);
                    }
                }
            }
        }
    }
}
